package com.happyhome.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.happyhome.constants.ComUnitName;
import com.happyhome.constants.RecordStatus;
import com.happyhome.datamodel.QDistrict;
import com.happyhome.datamodel.QHouse;
import com.happyhome.datamodel.QRoom;
import com.happyhome.datamodel.Room;
import com.mysema.query.BooleanBuilder;
import com.mysema.query.jpa.JPQLQuery;
import com.mysema.query.jpa.impl.JPAQuery;
import com.trg.dao.jpa.GenericDAOImpl;

@Repository
public class RoomDaoImpl extends GenericDAOImpl<Room, Long> implements RoomDao{
	
	@SuppressWarnings("unused")
	private static final Logger LOGGER = LoggerFactory.getLogger(RoomDaoImpl.class);
	
	@Override
	@PersistenceContext(unitName = ComUnitName.COM_UNIT)
	public void setEntityManager(EntityManager entityManager) {
		super.setEntityManager(entityManager);
	}
	
	@Override
	public List<Room> getRoomsData(Long limit) {
		JPQLQuery query = new JPAQuery(em());

		QRoom room = QRoom.room;

		BooleanBuilder where = new BooleanBuilder();

		where.and(room.status.eq(RecordStatus.TRUE));

		query.from(room)
			.where(where)
			.orderBy(room.makeDate.desc()).limit(limit);

		List<Room> products = query.list(room);
		return products;
	}
	
	@Override
	public List<Room> getListRoomsInListId(List<Long> listRoomsId) {
		JPQLQuery query = new JPAQuery(em());
		QRoom room = QRoom.room;
		
		query.from(room);
		
		BooleanBuilder where = new BooleanBuilder();
		
		where.and(room.status.eq(RecordStatus.TRUE))
			 .and(room.idRoom.in(listRoomsId));
		
		query.where(where);
		
		List<Room> resultRooms = query.list(room);
		return resultRooms;
	}

	@Override
	public List<Room> getListRoomsNotInListId(List<Long> listRoomsId) {
		JPQLQuery query = new JPAQuery(em());
		QRoom room = QRoom.room;
		
		query.from(room);
		
		BooleanBuilder where = new BooleanBuilder();
		
		where.and(room.status.eq(RecordStatus.TRUE))
			 .and(room.idRoom.notIn(listRoomsId));
		
		query.where(where);
		
		List<Room> resultRooms = query.list(room);
		return resultRooms;
	}

	@Override
	public List<Room> getListRoomsIsDisplay() {
		JPQLQuery query = new JPAQuery(em());
		QRoom room = QRoom.room;
		
		query.from(room);
		
		BooleanBuilder where = new BooleanBuilder();
		
		where.and(room.status.eq(RecordStatus.TRUE))
			 .and(room.isDisplay.eq(RecordStatus.TRUE));
		
		query.where(where);
		
		List<Room> resultRooms = query.list(room);
		return resultRooms;
	}

	@Override
	public List<Room> getListRoomsByHouseId(Long idHouse, Integer limit) {
		JPQLQuery query = new JPAQuery(em());

		QRoom room = QRoom.room;
		QHouse house = QHouse.house;
		
		BooleanBuilder where = new BooleanBuilder();

		where.and(room.status.eq(RecordStatus.TRUE))
			 .and(house.status.eq(RecordStatus.TRUE))
			 .and(room.house.idHouse.eq(idHouse));

		query.from(room).leftJoin(room.house, house).where(where).limit(limit);

		List<Room> products = query.list(room);
		return products;
	}

	@Override
	public List<Room> getListRoomByDistrict(List<Long> idDistricts,
			Long minCost, Long maxCost, Long limit) {
		JPQLQuery query = new JPAQuery(em());

		QRoom room = QRoom.room;
		QHouse house = QHouse.house;
		QDistrict district = QDistrict.district;
		
		BooleanBuilder where = new BooleanBuilder();

		where.and(room.status.eq(RecordStatus.TRUE))
			 .and(house.status.eq(RecordStatus.TRUE))
			 .and(district.status.eq(RecordStatus.TRUE));
		
		if(minCost != null && maxCost != null){
			where.and(room.priceRoom.goe(minCost)).and(room.priceRoom.loe(maxCost));
		}

		if(idDistricts != null && idDistricts.size() > 0){
			where.and(district.idDistrict.in(idDistricts));
		}
		
		query.from(room).leftJoin(room.house, house).leftJoin(house.district, district).where(where).limit(limit);

		List<Room> products = query.list(room);
		return products;
	}
}
